<%@page import="java.util.List"%>
<%@page import="com.winexpress.product.Category"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.winexpress.util.SessionAttributes"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>wineXpress Admin Manage Category</title>
    </head>
    <body>
        <t:adminNavigation/>
        <div class="container">
            <br><br>
            <h3 style="text-align: center;">Category Management</h3>
            <div style="width:82%; margin-left: 8%">
                <form id="search" action="/WineXpressClient/searchManagement/manageWines" method="post">
                    <div style="width: 45%" class="col-lg-6">
                        <select class="form-control" name="search">
                            <option value="searchCategory">Search Category by Name</option>
                            <option value="searchByWineName">Search Wine by Wine name</option>
                            <option value="searcyByCategory">Search Wine by Category</option>
                            <option value="searchByCountry">Search Wine by Country</option>
                            <option value="searchByRegion">Search Wine by Region</option>
                            <option value="searchByVarietal">Search Wine by Varietal</option>
                            <option value="searchByVintage">Search Wine by Vintage</option>
                        </select>
                    </div>
                    <div style="float: left; width: 45%; padding-left: 10px; padding-right: 10px;" class="col-lg-6">
                        <input type="text" class="form-control" name="key" placeholder="Enter keywords">
                    </div>
                    <button type="submit" class="btn btn-default" style="width: 10%;">Search</button>
                </form>
            </div>
            <br><br>

            <form class="form-horizontal" role="form" id="addNewCategory"  name="addCategory" action="/WineXpressClient/categoryManagement/manageCategory" method="post">
                <div class="col-sm-10 " style="width:70%; margin-left: 8%;">
                    <input type="text" class="form-control" name="newCategory" placeholder="New category"/>
                    <input name="add" id="addID" type="hidden" value="addCategory"/>
                </div> 
                <button type="submit" class="btn btn-default">Add new category</button>
            </form>
            
            <div class="panel-body">
                <% Boolean isDelete = (Boolean) request.getAttribute("isDelete");%>
                <% Boolean isAdd = (Boolean) request.getAttribute("isAdd");%>
                <% Boolean isEdit = (Boolean) request.getAttribute("isEdit");%>
                <%if (isDelete != null && isDelete) { %>
                <div class="alert alert-success"style="width: 80%; margin-left: 10%; margin-right: 10%" >The category is deleted.</div>
                <% } else if (isDelete != null && !isDelete) { %>
                <div class="alert alert-danger" style="width: 80%; margin-left: 10%; margin-right: 10%">Fail to delete. There are wines belonging to the category.</div>
                <% } else if (isAdd != null && !isAdd) {%>
                <div class="alert alert-danger" style="width: 80%; margin-left: 10%; margin-right: 10%">Duplicated category. Please enter another category.</div>
                <% } else if (isEdit != null && !isEdit) {%>
                <div class="alert alert-danger" style="width: 80%; margin-left: 10%; margin-right: 10%">Duplicated category name.Please try with another category name.</div>
                <% } %>
            </div>

            <table class="table" style='width:80%; margin-left: 10%; margin-right: 10%'>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Category Name</th>
                        <th style='white-space: nowrap;' width="15"> </th>
                        <th style='white-space: nowrap;' width="10"> </th>
                    </tr>
                </thead>

                <tbody>
                    <%  List<Category> list = (List) session.getAttribute("categoryList");%>
                    <% if (!list.isEmpty()) {
                            for (Object o : list) {
                                Category c = (Category) o;%>
                    <tr>
                        <td> <%=c.getCategoryID()%> </td>
                        <td><a href="/WineXpressClient/searchManagement/manageWines?search=searcyByCategory&key=<%=c.getName()%>"><%=c.getName()%></a></td>
                        <td align="right" style='white-space: nowrap;'>
                            <form class="form-horizontal" role="form" id="deleteCategory"  name="deletCategory" action="/WineXpressClient/categoryManagement/manageCategory" method="post"> 
                                <button type="submit" class="btn btn-default" >Delete</button>                          
                                <input name="delete" id="delete" type="hidden" value="delete"/>
                                <input name="deleteID" id="deleteID" type="hidden" value="<%=c.getCategoryID()%>"/>
                            </form></td>
                        <td align="right" style='white-space: nowrap;'>
                            <button class="btn btn-default" data-toggle="modal" data-target="#editModal<%=c.getCategoryID()%>"> Edit</button> 
                            <div class="modal fade" id="editModal<%=c.getCategoryID()%>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Edit</h4>
                                        </div>
                                        <form class="form-horizontal" role="form" id="editForm"  name="editCategory" action="/WineXpressClient/categoryManagement/manageCategory" method="post">
                                            <div class="modal-body">
                                                <input type="text" class="form-control" name="editName" placeholder="Enter new categroy name">
                                            </div>
                                            <div class="modal-footer">
                                                <button type="submit" class="btn btn-primary">Save</button>
                                                <input name="edit" id="edit" type="hidden" value="edit"/>
                                                <input name="editID" id="editID" type="hidden" value="<%=c.getCategoryID()%>"/>
                                                <br><br>
                                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>   
                    <%}
                } else {%>
                    <tr>
                <div class="alert alert-info"style="width: 80%; margin-left: 10%; margin-right: 10%" >No Category in the database.</div>             
                </tr>
                <%}%>
                </tbody>
            </table>
        </div>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#editForm').bootstrapValidator({
                    message: 'This value is not valid',
                    fields: {
                        editName: {
                            validators: {
                                notEmpty: {
                                    message: 'This field can\'t be empty.'
                                }
                            }
                        }
                    }
                })
            })
        </script>
        <t:footer/>
    </body>
</html>